<?php
namespace app\api\controller;
use app\api\model\CreditModel;
use app\api\model\SmModel;
use think\Request;
use think\Db;
class Credit extends BaseController
{
	public function __construct()
    {
        parent::__construct();
        $this->Credit = new CreditModel();
    }
	//首页信用显示
	//传值:用户id
	//返回:信用个数number
	public function index(){
		$id=$this->uid;
		$number=$this->Credit->getOneId($id);
		 AjaxReturn(1, "成功",$number);
	}
  
  	public function user()
    {
        $id=$this->uid;
        $number=$this->Credit->alias('u')->join('xst_balance b','u.id=b.uid')->where('u.id',$id)->find();
        AjaxReturn(1, "成功",$number);
    } 
  
	//二维码生成显示
	
	/*/实名认证
	//传值:真实姓名name、身份证号card 、手机号 phone、认证费用cost
	public function reals(){
		$id=$this->uid;
		$name=input('name');
		$card=input('card');
		$phone=input('phone');
		$cost=input('cost');
		$flg=$this->Credit->reals($id,$name,$card,$phone,$cost);
		if($flg){
			 AjaxReturn(1, "成功");
		}else{
			AjaxReturn(-1, "失败");
		}
	}*/
    //我的资产
	//传值:用户id
	//返回:总资产num 流通币 circulate循环币circulation
	public function balance(){
		$id=$this->uid;
		$res=$this->Credit->balance($id);
		$res['num']=$res['circulate']+$res['circulation'];
		if($res){
			 AjaxReturn(1, "成功",$res);
		}else{
			AjaxReturn(-1, "失败",$res);
		}
	}
    //流通币流通详情
	//传值:用户id
	//返回:数量balance时间createtime数量(sign 1加 2减)变动类型type(1：转入，2转出，3提现，4：买入，5卖出，6：购买商品)
	public function circulate(){
		$uid=$this->uid;
		$res=$this->Credit->circulate($uid);
		foreach($res as $key=>$val){
			$res[$key]['createtime']=date('Y-m-d H:i',$res[$key]['createtime']);
		}
		if($res){
			 AjaxReturn(1, "成功",$res);
		}else{
			AjaxReturn(-1, "失败",$res);
		}
	}
	//个人资料
	//传值:用户id、
	//返回:性别sex 地区area昵称nickname 头像Picture 手机号phone
	public function Personal (){
		$id=2;
		$res=$this->Credit->Personal($id);
		$res['birthday']=date('Y-m-d H:i',$res['birthday']);
		if($res['sex']==1){
			$res['sex']='男';
		}
		if($res['sex']==2){
			$res['sex']='女';
		}
		if($res['sex']==3){
			$res['sex']='保密';
		}
		if($res){
			 AjaxReturn(1, "成功",$res);
		}else{
			AjaxReturn(-1, "失败",$res);
		}
		
	}
	//个人资料修改昵称
    //传值:用户id、昵称nickname
	//返回:成功1失败-1
	public function nickname(){
		$uid=$this->uid;
		$nickname=input('nickname');
		$res=$this->Credit->nickname($uid,$nickname);
		if($res){
			 AjaxReturn(1, "修改成功");
		}else{
			AjaxReturn(-1, "修改失败");
		}
	}
	//个人资料修改性别
    //传值:用户id、性别sex(男、女、保密)
	//返回:成功1失败-1
	public function sex(){
		$uid=$this->uid;
		$sex=input('sex');
		if($sex=='男'){
			$sex=1;
		}
		if($sex=='女'){
			$sex=2;
		}
		if($sex=='保密'){
			$sex=3;
		}
		$res=$this->Credit->sex($uid,$sex);
		if($res){
			 AjaxReturn(1, "修改成功");
		}else{
			AjaxReturn(-1, "修改失败");
		}
	}
	//个人资料修改生日
    //传值:用户id、生日birthday
	//返回:成功1失败-1
	public function birthday(){
		$uid=$this->uid;
		$birthday=input('birthday');
		$res=$this->Credit->birthday($uid,$birthday);
		if($res){
			 AjaxReturn(1, "修改成功");
		}else{
			AjaxReturn(-1, "修改失败");
		}
	}
	//个人资料修改地区
    //传值:用户id、地区area
	//返回:成功1失败-1
	public function area(){
		$uid=$this->uid;
		$area=input('area');
		$res=$this->Credit->area($uid,$area);
		if($res){
			 AjaxReturn(1, "修改成功");
		}else{
			AjaxReturn(-1, "修改失败");
		}
	}
  	
    //个人资料解绑手机号
    //传值:用户id、手机号phone
    //返回:成功1失败-1
    public function relieve(){
        $uid=$this->uid;
        $phone=input('phone');
        $code=input('code');
        $sm = new SmModel();
        $res = $sm->where('phone',$phone)
            ->order('id desc')
            ->limit(0,1)
            ->select();
        //if($res[0]['code']!=$code&&$res[0]['invalidtime']>=time())
      	if('123456'!=$code)
        {
            return $stu = ['code'=>0,'message'=>'验证码不正确'];
        }else{
            $res=$this->Credit->relieve($uid,$phone);
            if($res){
                AjaxReturn(1, "修改成功");
            }else{
                AjaxReturn(-1, "修改失败");
            }
        }
    }
	//个人资料修改手机号
    //传值:用户id、手机号phone
	//返回:成功1失败-1
	public function phone(){
		$uid=$this->uid;
		$phone=input('phone');
		$res=$this->Credit->phone($uid,$phone);
		if($res){
			 AjaxReturn(1, "修改成功");
		}else{
			AjaxReturn(-1, "修改失败");
		}
	}
	//个人资料修改头像
    //传值:用户id、头像phone
	//返回:成功头像路径Picture失败-1
	public function picture(){
		$uid=$this->uid;
		$file = request()->file('file');
      	
		$files=$this->Credit->upload($file);
		$res=$this->Credit->Picture($uid,$files);
		if($res){
			$Picture=Db::name('user')->where('id',$uid)->value('Picture');
			$request = Request::instance();
		    //$domain=$request->domain();
			//$Picture=$domain.'/upload/Picture/'.$Picture;
			 AjaxReturn(1, "修改成功",$Picture);
		}else{
			AjaxReturn(-1, "修改失败");
		}
	}
	//钱包地址
    public function wallet()
    {
        $uid = $this->uid;
        $data = $this->Credit->wallet($uid);
        if(!empty($data))
        {
            AjaxReturn('1','成功',$data);
        }else{
            AjaxReturn('0','失败');
        }
    }
    //转账
    public function transsfer()
    {
        $uid = $this->uid;
        if(input('post.'))
        {
            $data=input();
//            if($data["numbers"]<100)
//            {AjaxReturn('2','最小转账金额100');
//            }
//            if($data["numbers"]%100!=0)
//            {
//                AjaxReturn('2','转账金额为100的倍数');
//            }
            $transsfer = $this->Credit->transsfer($uid);
            if($transsfer=='10000')
            {AjaxReturn('2','钱包密码不正确');
            }elseif($transsfer=='10003')
            {
                AjaxReturn('3','不能给自己账号转账');
            }elseif($transsfer=='10001'){
                AjaxReturn('1','转账成功');
            }elseif($transsfer=='10002'){
                AjaxReturn('0','转账失败');
            }elseif($transsfer=='10004'){
                AjaxReturn('4','转出数量不能大于余额');
            }elseif($transsfer=='20001'){
                AjaxReturn('5','不能转给自己');
            }elseif($transsfer=='20002'){
                AjaxReturn('6','没有该用户');
            }elseif($transsfer=='20003'){
                AjaxReturn('6','您的以太坊余额不足');
            }
        }
        AjaxReturn('0','参数错误！');
    }
	//eth兑换xst
    public function exchange_xst(){
        $uid = $this->uid;
        $res = $this->Credit->exchange_xst($uid);
        return $res;
    }
    //xst兑换eth
    public function exchange_eth(){
        $uid = $this->uid;
        $res = $this->Credit->exchange_eth($uid);
        return $res;
    }
  //提币
    public function withdrawal_eth(){
        $uid = $this->uid;
        $res = $this->Credit->withdrawal_eth($uid);
        return $res;
    }
     public function recommend()
    {
        $uid=$this->uid;
        $phone = request()->post('phone');
        $data = $this->Credit->recommend($uid,$phone);
        if($data)
        {
            AjaxReturn('1','成功',$data);
        }else{
            AjaxReturn('0','暂无更多数据');
        }
    }
  //复投
    public function Double_throw(){
        $uid = $this->uid;
        $res = $this->Credit->double_throw($uid);
        return $res;
    }
    //领取红包
    public function Get_a_red_envelope()
    {
        $uid = $this->uid;
        $res = $this->Credit->Get_a_red_envelope($uid);
        return $res;
    }
  //判断是否领过红包
    public function is_get_redenvelope()
    {
        $uid = $this->uid;
        $res = $this->Credit->is_get_redenvelope($uid);
        return $res;
    }
  //红包记录
    public function redenvelope_log()
    {
        $uid = $this->uid;
        $res = $this->Credit->redenvelope_log($uid);
        return $res;
    }
  	public function wechat()
    {
        $uid = $this->uid;
        $data = Db::name('wechat')->where('uid',$uid)->find();
        if($data)
        {
            AjaxReturn('1','成功',$data);
        }else{
            AjaxReturn('0','未绑定');
        }
    }
    public function alipay()
    {
        $uid = $this->uid;
        $data = Db::name('alipay')->where('uid',$uid)->find();
        if($data)
        {
            AjaxReturn('1','成功',$data);
        }else{
            AjaxReturn('0','未绑定');
        }
    }
    public function bank()
    {
        $uid = $this->uid;
        $data = Db::name('bank')->where('uid',$uid)->find();
        if($data)
        {
            AjaxReturn('1','成功',$data);
        }else{
            AjaxReturn('0','未绑定');
        }
    }
   public function reg_url()
    {
        $uid = $this->uid;
        $data = Db::name('user')->where('id',$uid)->find();
        if($data)
        {
            $url = "http://xst.nowdoing.com.cn/register/index.html?code=".$data['code'];
            AjaxReturn('1','成功',$url);
        }else{
            AjaxReturn('0','未绑定');
        }
    }
}
?>